﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using Northwind.Application;
using Northwind.Interfaces;
using Northwind.Interfaces.Data.Model;
using System.Linq;
using Northwind.ViewModel.Messeges;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Threading;
using System;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Northwind.Application.Model;

namespace Northwind.ViewModel
{
    public class OrdersViewModel : ToolViewModel
    {
        #region Fields

        private readonly ICustomerModel _customer;

        #endregion Fields

        #region Properties

        public const string OrderIDPropertyName
            = "OrderID";

        private string _orderID;

        public string OrderID
        {
            get
            {
                return _orderID;
            }

            set
            {
                if (string.Compare(_orderID, value, true) == 0)
                {
                    return;
                }

                _orderID = value;
                RaisePropertyChanged(OrderIDPropertyName);
                RaisePropertyChanged(IsFindEnabledPropertyName);
            }
        }

        private string _orderHelp = "Enter order ID of order to find...";
        
        public string OrderHelp
        {
            get { return _orderHelp; }
        }

        public const string IsFindEnabledPropertyName = "IsFindEnabled";

        public bool IsFindEnabled
        {
            get { return !string.IsNullOrEmpty(OrderID); }
        }

        #endregion Properties

        #region Commands

        private ICommand _showOrderCommand;
        public ICommand ShowOrderCommand
        {
            get
            {
                if (_showOrderCommand == null)
                {
                    _showOrderCommand =
                        new RelayCommand(
                            () =>
                            {
                                Messenger.Default.Send<ShowOrder>(
                                    new ShowOrder()
                                        {
                                            OrderID
                                              = OrderID
                                        });
                            });

                }

                return _showOrderCommand;
            }
        }

        #endregion

        #region Ctors

        public OrdersViewModel(ICustomerModel customer)
            : base(customer.CompanyName)
        {
            _customer = customer;
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
            }
            else
            {
                // Code runs "for real": Connect to service, etc...
                //from ICustomerModel localCustomer in Core.Instance.NorthwindManager.GetAllCustomersNameAndID(true)
                //    select localCustomer;

            }
        }

        public OrdersViewModel()
        {
            Name = "Orders";
        }

        #endregion Ctors

        #region EventHandlers



        #endregion EventHandlers

        #region CommandHandlers



        #endregion CommandHandlers

        #region Helpers



        #endregion Helpers

        #region Public Methods

        //public void CancelOrder(IOrderModel order)
        //{
        //    Orders.Remove(order);
        //    Messenger.Default.Send<OrderCancelled>(new OrderCancelled() { CancelledOrder = order });
        //}

        #endregion
    }
}